package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class SeckillController {

    @Autowired
    ActivityFeignClient activityFeignClient;

    @GetMapping("seckill.html")
    public String index(Model model){
        Result result = activityFeignClient.findAll();
        model.addAttribute("list",result.getData());
        return "seckill/index";
    }
    @GetMapping("seckill/{skuId}.html")
    public String getItem(@PathVariable Long skuId,Model model){
        Result result = activityFeignClient.getSeckillGoods(skuId);
        model.addAttribute("item",result.getData());
        return "seckill/item";
    }
    //秒杀排队测试
    @GetMapping("queue.html")
    public String queue(HttpServletRequest request){
        //获取skuId
        String skuId = request.getParameter("skuId");
        //获取下单码
        String skuIdStr = request.getParameter("skuIdStr");
        return "seckill/queue";
    }
    //确定订单
    @GetMapping("seckill/trade.html")
    public String trade(Model model){
        Result<Map<String, Object>> trade = activityFeignClient.trade();

        if(trade.isOk()){
            model.addAllAttributes(trade.getData());
            return "seckill/trade";
        }else {
            model.addAttribute("message",trade.getMessage());
            return "seckill/fail";
        }
    }
}
